#include "Vertex.h"
#include "Face.h"

/************************************************/
/*				Statics 						*/
/************************************************/

    /************************************************/
    /*				Properties 						*/
    /************************************************/

        std::vector<Vertex *>   Vertex::Vertices;

    /************************************************/
    /*				Methods 						*/
    /************************************************/

        Vertex *  Vertex::InitNew(vec4 * position, vec4 * color, vec4*  normal, vec4 * texcoord0)
        {
            Vertex * vertex = new Vertex(position, color, normal, texcoord0);
            Vertices.push_back(vertex);
            return vertex;
        }

        Vertex* Vertex::New()
        {
            return InitNew(new vec4(), new  vec4(), new vec4(), new vec4());
        }

        Vertex * Vertex::New(vec4 * position)
        {
            return InitNew(position, new vec4(), new vec4(), new vec4());
        }

        Vertex * Vertex::New(vec4 * position, vec4 * color)
        {
            return InitNew(position, color, new vec4(), new  vec4());
        }

        Vertex * Vertex::New(vec4 * position, vec4 * color, vec4 * normal)
        {
            return InitNew(position, color, normal, new vec4());
        }

        Vertex * Vertex::New(vec4 * position, vec4 * color, vec4 * normal, vec4 * texcoord0)
        {
            return InitNew(position, color, normal, texcoord0);
        }


    float * Vertex::Array()
    {
        std::vector<float> vecArray;

        for(int i=0;i<Vertices.size();i++)
        {
            Vertices[i]->m_Index=i;

            for(int j=0; j<Vertices[i]->m_Attributes->m_Attributes.size();j++)
            {
                vecArray.push_back(Vertices[i]->m_Attributes->m_Attributes[j]->x);
                vecArray.push_back(Vertices[i]->m_Attributes->m_Attributes[j]->y);
                vecArray.push_back(Vertices[i]->m_Attributes->m_Attributes[j]->z);
                vecArray.push_back(Vertices[i]->m_Attributes->m_Attributes[j]->w);
            }
        }

        float * returnArray = new float[vecArray.size()];

        for(int i=0;i<vecArray.size();i++)
        {
            returnArray[i]=vecArray[i];
        }

        return returnArray;
    }

/************************************************/
/*				Members 						*/
/************************************************/

    void Vertex::ComputeNormal()
    {
        vec4 sum;

        for(int i=0;i<m_Faces.size();i++)
        {
            sum+=m_Faces.at(i)->m_Normal;
        }

        sum= sum/m_Faces.size();
 //       m_Normal = (sum).Normalize();
    }

    /************************************************/
    /*				Constructors					*/
    /************************************************/


    Vertex::Vertex(vec4 * position)
    {
        Init(position, new vec4(), new vec4(), new vec4());
    }

    Vertex::Vertex(vec4 * position, vec4 * color)
    {
        Init(position, color, new vec4(), new vec4());
    }

    Vertex::Vertex(vec4 * position, vec4 * color, vec4 * normal)
    {
        Init(position, color, normal, new vec4());
    }

    Vertex::Vertex(vec4 * position, vec4 * color, vec4 * normal, vec4 * texcoord0)
    {
        Init(position, color, normal, texcoord0);
    }

    void Vertex::Init(vec4 * position, vec4 * color, vec4 * normal, vec4 * texCoord)
    {
        m_Attributes= new Attributes(position, color, normal, texCoord);
    }

